package com.demo.aviator.controller;

import com.demo.aviator.service.IExpress;
import com.demo.aviator.spi.IAnimal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import sun.misc.Service;

import java.util.Iterator;
import java.util.ServiceLoader;

/**
 * @author lgq
 * https://www.jianshu.com/p/02403dd1f4c4
 * https://blog.csdn.net/keda8997110/article/details/50782848
 */
@RestController
@RequestMapping("/aviator")
public class TestController {

    @Autowired
    private IExpress express;

    @PostMapping("/arithmetic")
    public String doTry1(){
        return express.arithmetic();
    }

    @PostMapping("/logic")
    public String doTry2(){
        return express.logic();
    }

    @PostMapping("/complex")
    public String doTry3(){
        return express.cpmplex();
    }

    @PostMapping("/complexExc")
    public String doTry4(){
        return express.cpmplexExc();
    }

    @PostMapping("/threeExp")
    public String doTry5(){
        return express.threeExp();
    }

    @PostMapping("/callFunction")
    public String doTry6(){
        return express.callFunction();
    }

    @PostMapping("/regular")
    public String doTry7(){
        return express.regular();
    }

    @PostMapping("/grammar")
    public String doTry8(){
        return express.grammar();
    }

    /**
     * https://www.jianshu.com/p/77b99fd9d8b6
     * @return
     */
    @PostMapping("/spi")
    public String doTry9(){
        String classNames = null;
        Iterator<IAnimal> animalIterator = Service.providers(IAnimal.class);
        ServiceLoader<IAnimal> animals = ServiceLoader.load(IAnimal.class);
        while(animalIterator.hasNext()) {
            IAnimal ser = animalIterator.next();
            classNames += ser.whatYourName();
        }
        Iterator<IAnimal> iterator = animals.iterator();
        while(iterator.hasNext()) {
            IAnimal ser = iterator.next();
            classNames += ser.whatYourName();
        }
        return classNames;
    }
}
